clear
clc
close all
PTime=[2.5 3.6 4.0 2.7 1.5 4.4 4.0;3.7 2.9	4.0	2.9	2.6	2.2	3.9;2.1	3.1	2.0	3.0	1.2	2.7	3.5;2.5	2.5	4.0	1.4	1.7	3.6	4.9;4.0	2.0	4.0	2.9	1.6	2.2	2.5;2.4	3.2	4.0	2.2	2.6	2.0	4.2;4.0	3.5	3.0	1.8	1.4	2.5	2.0;1.3	2.3	2.0	1.6	2.1	2.9	3.6;3.1	3.4	4.0	1.2	2.4	4.3	4.0;1.6	2.9	3.0	1.7	2.6	4.5	2.0;3.7	2.5	4.0	1.4	2.9	4.3	3.9;2.7	3.7	4.0	1.7	1.2	3.0	3.4;1.3	4.0	3.0	2.3	2.2	4.1	4.7;];

%%  参数设置

SearchAgents = 25;                        % population members 
Max_iterations = 20;                    % maximum number of iteration
lb = 0;
ub = 1;
dim = 13;
fobj = @my_fobj;
%%  调用算法
%[Alpha_score,FTTA_pos,FTTA_Convergence_curve,R,gene_FTTA]=FTTA_1(SearchAgents,Max_iterations,lb,ub,dim,fobj,PTime); % 调用FTTA算法
% [SA_score,SA_pos,SA_Convergence_curve,gene_SA]=SA_1(Max_iterations,lb,ub,dim,PTime); % 调用SA算法
% SA_score = - SA_score;
[GA_score,GA_pos,GA_Convergence_curve,gene_GA]=GA_1(SearchAgents,Max_iterations,PTime,dim); % 调用GA算法
%[group_condition,groupid,itemRP,R,new_PTime,Bin,Item] =  BL_2DBP(FTTA_pos,PTime);
%[PNumber,MNumber]=size(new_PTime);  
%[FTTA_TCost,Mspan,Scheme,BginTime]=my_cal(new_PTime,gene_FTTA);
[group_condition,groupid,itemRP,R,new_PTime,Bin,Item] =  BL_2DBP(GA_pos,PTime);
[PNumber,MNumber]=size(new_PTime);  
[GA_TCost,Mspan,Scheme,BginTime]=my_cal(new_PTime,gene_GA);


CNT=15;
k=round(linspace(1,Max_iterations,CNT)); %随机选CNT个点
% 注意：如果收敛曲线画出来的点很少，随机点很稀疏，说明点取少了，这时应增加取点的数量，100、200、300等，逐渐增加
% 相反，如果收敛曲线上的随机点非常密集，说明点取多了，此时要减少取点数量
% iter=1:1:Max_iterations;
%  plot(iter(k),SA_Convergence_curve(k),'b-*','linewidth',1);
%  hold on;
%  plot(iter(k),FTTA_Convergence_curve(k),'r->','linewidth',1);
%  hold on;
%  plot(iter(k),GA_Convergence_curve(k),'g-d','linewidth',1);
% 
% grid on;
% title('收敛曲线')
% xlabel('迭代次数');
% ylabel('适应度值');
% box on
% legend('SA','FTTA','GA')
% set (gcf,'position', [300,300,800,330])






%%%绘图  组合分配情况
for i = 1:groupid
    figure
    set(gca,'FontName','Times New Roman','FontSize',14)
    set(gcf,'Color','white')
    
    rectangle('position',[0,0,Bin(1),Bin(2)])  %其中，x,y代表矩形左下角坐标 w，h代表宽度、长度
    axis([0 Bin(1) 0 Bin(2)])
    for j = 1:13
      if group_condition(j,i) ~= 0

      rectangle('position',[itemRP(1,1,j)-Item(j,1),itemRP(1,2,j)-Item(j,2),...
                        Item(j,1),Item(j,2)],'FaceColor',[0 0.4470 0.7410],'EdgeColor','k')  %其中，x,y代表矩形左下角坐标 w，h代表宽度、长度
      text(itemRP(1,1,j)-Item(j,1)/2,itemRP(1,2,j)-Item(j,2)/2,num2str(j),'FontSize', 14);
      end

    end

end






%% 显示最优解 （甘特图）
figure;
set(gca,'FontName','Times New Roman','FontSize',24)
set(gcf,'Color','white')
xlabel('processing time(h)'); 
ylabel('group id'); 
% axis([0,max(max(Mspan)),0,PNumber]);                  %x轴 y轴的范围
set(gca,'xtick',0:20:max(max(Mspan))) ;%x轴的增长幅度????时间的间隔待定!!!!!!!!!!!!!!!!
set(gca,'ytick',0:1:PNumber);         %y轴的增长幅度
set(gca,'Box','on');                  %图形的右边和上边有实线，封闭
%set(gca,'YTickLabel',{'';num2str((1:PNumber)','Element%d');''});  %y轴的标签修改为“element *"格式
title('Gantt chart');                 %图形的标题


rec=[0,0,0,0];%所画矩形的属性初始化，即坐标及长宽
for i =1:PNumber
    for j=1:MNumber
  rec(1) = BginTime(i,j);%所画矩形的横坐标x,工序开始时间
  rec(2) = i-0.2;  %所画矩形的起始纵坐标y，即在哪一行画线
  rec(3) = (Mspan(i,j)- BginTime(i,j));  %所画矩形的x轴方向长度,工序的持续时间
  rec(4) = 0.6; %所画矩形的y轴方向高度
 %txt=sprintf('N%d',j);%显示内容为“N工序名”
  rectangle('Position',rec,'LineWidth',0.5,'LineStyle','-');%draw every rectangle  

  %text(BginTime(i,j)+0.5,i+0.2,txt);%字体的坐标和内容
   %%%%%%%%%%%%%%%%%%%%%%%如果每个矩形内容显示不全，增大x轴间距!!!!!!!!!!!!!!!!!!!
    end
end







